﻿using System;
using LevelEditor.Document;
using LevelEditor.Forms;

namespace LevelEditor.View
{
    public class DoUndoAddTileSet : DoUndoNothing
    {
        #region Fields

        int LayerIndex;
        int Index;
        TileSet TileSet;

        #endregion

        #region Constructors

        public DoUndoAddTileSet(MainForm form, TileSet tileSet):base(form)
        {
            if (tileSet == null)
                throw new NullReferenceException();
            LayerIndex = form.CurrentLayer;
            Index = form.Level.Layers[LayerIndex].TileSets.Count;
            TileSet = tileSet;
        }

        #endregion

        #region Methods

        public override bool Do()
        {
            MainForm.Level.Layers[LayerIndex].TileSets.Add(new TileSet(TileSet));
            MainForm.CurrentLayer = LayerIndex;
            MainForm.RefreshControlData();
            MainForm.Palette.tabs.SelectedIndex = MainForm.Palette.tabs.TabPages.Count - 1;
            return base.Do();
        }

        public override void Undo()
        {
            MainForm.Level.Layers[LayerIndex].TileSets.RemoveAt(Index);
            MainForm.CurrentLayer = LayerIndex;
            MainForm.RefreshControlData();
        }

        public override string ToString()
        {
            return "Add tile set " + TileSet.ResourceName.ToString();
        }

        #endregion
    }
}
